package com.wifi.a007.utile;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.wifi.a007.db.City;
import com.wifi.a007.db.County;
import com.wifi.a007.db.Province;
import com.wifi.a007.json.Weather;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by Administrator on 2017/7/6.
 */

public class Utility {

        public static boolean handleProvinceResponce(String response){


            Log.i("response5699999999999",response);
            if(!TextUtils.isEmpty(response)){//null kong

                try{

                    JSONArray allProvinces =new JSONArray(response);
                    for(int i=0; i < allProvinces.length(); i++){

                        JSONObject provinceObject =allProvinces.getJSONObject(i);
                        Province province = new Province();
                        province.setProvinceName(provinceObject.getString("name"));
                        province.setProvinCode(provinceObject.getInt("id"));
                        province.save();
                    }

                    return true;

                }catch(JSONException e){

                    e.printStackTrace();
                }

            }

            return false;
        }

    public static boolean handleCityResponse(String response, int provinceId){

        if(!TextUtils.isEmpty(response)){
            try{

                JSONArray allCities= new JSONArray(response);
                for(int i=0; i< allCities.length(); i++){

                    JSONObject cityobject = allCities.getJSONObject(i);

                    City city = new City();
                    city.setCityName(cityobject.getString("name"));
                    city.setCityCode(cityobject.getInt("id"));
                    city.setProvinceId(provinceId);
                    city.save();

                }

                return true;

            }catch(JSONException e){

                e.printStackTrace();
            }



        }

        return false;

    }

    public static boolean handleCountyResponse(String response, int cityId){

        if(!TextUtils.isEmpty(response)){

            try{

                JSONArray allCounties = new JSONArray(response);

                for(int i=0; i< allCounties.length(); i++){

                    JSONObject countyObject = allCounties.getJSONObject(i);
                    County county  = new County();
                    county.setCountyName(countyObject.getString("name"));
                    county.setWeatherId(countyObject.getString("weather_id"));
                    county.setCityId(cityId);
                    county.save();
                }

                return true;


            }catch (JSONException e){
                e.printStackTrace();
            }

        }

        return  false;

    }



    public static Weather handleWeatherResponse(String response){

        try{

            Log.i("response",response);

            JSONObject jsonObject= new JSONObject(response);

            JSONArray jsonArray = jsonObject.getJSONArray("HeWeather");
            String weatherContent = jsonArray.getJSONObject(0).toString();
            //String  weatherContent1 = jsonArray.getJSONObject(1).toString();
            Log.i("weatherContetn",weatherContent);
            //Log.i("weatherContetn",weatherContent1);
            Log.i("jsongObject",jsonObject.toString());
            return new Gson().fromJson(weatherContent,Weather.class);




        }catch (Exception e){

            e.printStackTrace();
        }

        return null;

    }



}
